package com.hd.json2mysql.service;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.hd.json2mysql.entity.Classes;
import com.hd.json2mysql.enums.ClassType;
import com.hd.json2mysql.mapper.CodeAnalysisMapper;
import com.hd.json2mysql.utils.IdGenerator;
import com.hd.json2mysql.utils.JsonReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Service
public class FullService {
    @Autowired
    private CodeAnalysisMapper codeAnalysisMapper;

    @Autowired
    private FieldsService fieldsService;
    private final Map<String, String> classIdMap = new HashMap<>();

    @Transactional
    public void processClasses(String path, int systemId) {
        try {
            JsonNode jsonNode = JsonReader.readJson(path);

            if (jsonNode.isObject()) {
                ObjectNode objectNode = (ObjectNode) jsonNode;

                // 递归处理所有子节点
                objectNode.fields().forEachRemaining(entry -> {
                    storeClass((ObjectNode) entry.getValue(), systemId);
                });
            } else if (jsonNode.isArray()) {
                // 处理数组中的每个元素
                for (JsonNode arrayElement : jsonNode) {
                    ObjectNode objectNode = (ObjectNode) arrayElement;
                    storeClass(objectNode, systemId);
                    String nodeName = objectNode.get("NodeName").asText();
                    JsonNode fields = objectNode.get("Fields");
                    if (fields != null&&!fields.isEmpty()){
                        for (JsonNode field : fields){
                            if (nodeName != null && field != null && !field.isEmpty()) {
                                String classID = classIdMap.get(nodeName);
                                fieldsService.storeFields(field, classID);
                            }
                        }
                    }
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private void storeClass(ObjectNode classNode, int systemId) {

        Classes classes = new Classes();

        String classId = IdGenerator.generate32Id();

        classes.setId(classId);
        try {
            classes.setNodeName(classNode.get("NodeName").asText());
            classes.setModule(classNode.get("Module").asText());
            classes.setType(ClassType.valueOf(classNode.get("Type").asText()));
            classes.setPackageName(classNode.get("Package").asText());
            classes.setFilePath(classNode.get("FilePath").asText());
            classes.setSystem_id(systemId);
            // 其他字段转换...
            classes.setStartLine(classNode.get("Position").get("StartLine").asInt());
            classes.setEndLine(classNode.get("Position").get("StopLine").asInt());

        } catch (Exception e) {
            System.out.println("无此属性,跳过：" + e.getMessage());
        }

        codeAnalysisMapper.insertClass(classes);

        classIdMap.put(classNode.get("NodeName").asText(), classId);
    }


}
